package com.fatmeraid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fatmeraid.common.exception.FarmException;
import com.fatmeraid.common.login.LoginUser;
import com.fatmeraid.common.login.LoginUserHolder;
import com.fatmeraid.common.result.ResultCodeEnum;
import com.fatmeraid.common.utils.MinioUtil;
import com.fatmeraid.dao.ProductDao;
import com.fatmeraid.dao.ProductImageDao;
import com.fatmeraid.dao.UserDao;
import com.fatmeraid.domain.Product;
import com.fatmeraid.domain.ProductImage;
import com.fatmeraid.service.ProductImageService;

import com.fatmeraid.vo.ProductImage.UploadImageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductImageImpl implements ProductImageService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private ProductImageDao productImageDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private MinioUtil minioUtil;

	@Override
	public boolean uploadProductImage(UploadImageVo uploadImageVo) {
		// 查看当前权限
		LoginUser loginUser = LoginUserHolder.getLoginUser();
		Long userId = loginUser.getUserId();
		Integer roleType = userDao.selectById(userId).getRoleType();
		if (roleType != 2 && roleType != 3){
			throw new FarmException(ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);
		}

		Long productId = uploadImageVo.getProductId();
		String imageUrl = uploadImageVo.getImageUrl();
		Integer sortOrder = uploadImageVo.getSortOrder();

		if (imageUrl==null||imageUrl.isEmpty()){
			throw new FarmException(ResultCodeEnum.PARAM_ERROR);
		}

		ProductImage productImage = new ProductImage();
		productImage.setProductId(productId);
		productImage.setImageUrl(imageUrl);
		productImage.setSortOrder(sortOrder);

		int insert = productImageDao.insert(productImage);

		return insert>0;
	}

	@Override
	public List<ProductImage> getProductImageList(Long productId) {
		LambdaQueryWrapper<ProductImage> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ProductImage::getProductId, productId);
		List<ProductImage> productImages = productImageDao.selectList(lqw);
		return productImages;
	}

	@Override
	public boolean deleteProductImage(Long id) {
		// 查看当前权限
		LoginUser loginUser = LoginUserHolder.getLoginUser();
		Long userId = loginUser.getUserId();
		Integer roleType = userDao.selectById(userId).getRoleType();
		if (roleType != 2 && roleType != 3){
			throw new FarmException(ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);
		}
		// 判断一下删除的图片是不是属于当前用户
		ProductImage productImage = productImageDao.selectById(id);
		Long productId = productImage.getProductId();
		Product product = productDao.selectById(productId);
		if (!product.getFarmerId().equals(userId)){
			throw new FarmException(ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);
		}

		// 删除数据库中的图片
		int delete = productImageDao.deleteById(id);

		if (delete > 0){
			System.out.println("删除数据库中的图片成功");
			// 删除mioin中的图片
			String imageUrl = productImage.getImageUrl();
			String fileName = minioUtil.extractFileNameFromUrl(imageUrl);

			System.out.println("fileName:"+fileName);
			if (fileName != null) {
				// 将日期
				minioUtil.deleteFile(fileName);

			}
		}


		return delete > 0;
	}
}
